#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <math.h>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_pat_clicked()
{
    QString pattern = ui->lineEdit_regExpStr->text();
    QRegExp rx(pattern);
    QString tmpSource=ui->lineEdit_source->text();
//    QString tmpSource="Image\r\n[X:253.1;Y:190.02;A:87.2;ATTR:0;ID:8]\r\n";
    qDebug()<<"tmpSource"<<tmpSource;
//    programStr.indexOf(rx);

    bool isMatch=rx.exactMatch(tmpSource);
    qDebug()<<rx;

    if(false==isMatch )
    {
        ui->lineEdit_isSucess->setText("failed");
    }
    else
    {
        ui->lineEdit_isSucess->setText("sucess");
    }

    ui->textEdit_result->clear();
    for(int i=0;i<rx.matchedLength();i++)
    {
        ui->textEdit_result->append(rx.cap(i));
        qDebug()<<rx.cap(i);
    }

    qDebug()<<"tmpSource.indexOf(rx)"<<tmpSource.indexOf(rx);

}

void MainWindow::on_pushButton_patVisionData_clicked()
{
    QVector<CameraReturnInfo> resultOut;
    QString visionText=ui->textEdit_visionData->toPlainText();
    visionText="1 -10.0 25.36 45.9\r1 88.3 15.36 35.9\r";
    int tmpKey=processReceiveData_3value(visionText.toLatin1(),resultOut);

    if(1!=tmpKey )
    {
        ui->lineEdit_isSucess_vision->setText("failed");
    }
    else
    {
        ui->lineEdit_isSucess_vision->setText("sucess");
    }

    ui->textEdit_result_vision->clear();
    for(int i=0;i<resultOut.size();i++)
    {
        QString tmpStr="result:";
        tmpStr+=QString::number(resultOut[i].pointCount)+" x"+QString::number(resultOut[i].x)
                +" y"+QString::number(resultOut[i].y)+" r"+QString::number(resultOut[i].r);
        ui->textEdit_result_vision->append(tmpStr);
    }
}

int MainWindow::processReceiveData_3value(const QByteArray &dataIn,
                                           QVector<CameraReturnInfo> &resultOut)
{
//    qDebug()<<"VisionTrack::processReceiveData_3value";
    resultOut.clear();
    if(0==dataIn.size())
    {
//        qDebug()<<"0==dataIn.size()";
        return 0;
    }
    //拆分成多条
    QString tmp_frame=dataIn;

    for(int i=0;i<100;i++)
    {
        CameraReturnInfo tmpResult;
        QString tmpStr=tmp_frame.section("\r", i, i);

        if(0==tmpStr.size())
        {
            break;
        }
        else
        {
            tmpStr+="\r";
            if(1==processReceiveData_3value_single(tmpStr,tmpResult))
            {
                resultOut.append(tmpResult);
            }
        }
    }

    return 1;


}


int MainWindow::processReceiveData_3value_single(QString tmp_frame, CameraReturnInfo &resultOut)
{
    if(0==tmp_frame.size())
    {
//        qDebug()<<"0==dataIn.size()";
        return 0;
    }

    //末尾处理
    QString startFlag = "\r";
    int startIndex=tmp_frame.indexOf(startFlag);
    if(0>startIndex)
    {
        return -2;
    }
    tmp_frame=tmp_frame.mid(0,startIndex);

    //提取header
    startFlag = ' ';
    startIndex=tmp_frame.indexOf(startFlag);
    if(0>startIndex)
    {
        return -1;
    }

    resultOut.pointCount=tmp_frame.section(" ", 0, 0).toInt();
    resultOut.x=tmp_frame.section(" ", 1, 1).toDouble();
    resultOut.y=tmp_frame.section(" ", 2, 2).toDouble();
    resultOut.r=tmp_frame.section(" ", 3).toDouble();

    return 1;
}

int MainWindow::processReceiveData_keba(const QByteArray &dataIn, QVector<QVector<CameraReturnInfo> > &resultOut)
{
    //一个接收不允许有不同时间传过来的物体，因为锁存编码器不一样．
//    qDebug()<<"processReceiveData_keba";
    resultOut.clear();
    if(0==dataIn.size())
    {
//            qDebug()<<"0==dataIn.size()";
        return 0;
    }
//    qDebug()<<"VisionTrack::processReceiveData_keba"<<dataIn;
    //拆分成多条
    QString tmp_frame=dataIn;

    //done分割
    int tmpCount=tmp_frame.count("Done");
    if(0>=tmpCount)
    {
        return -1;
    }
    for(int i=0;i<tmpCount;i++)
    {

        QString tmpStr=tmp_frame.section("Done", i, i);

        if(0==tmpStr.size())
        {
            continue;
        }
        else
        {
//                tmpStr+="\r";
            QVector<CameraReturnInfo> tmpResult;
            if(1==processReceiveData_keba_oneTime(tmpStr,tmpResult))
            {
                resultOut.append(tmpResult);
            }
            else
            {
                CameraReturnInfo tmpPoint;
                tmpPoint.pointCount=0;
                tmpPoint.x=0;
                tmpPoint.y=0;
                tmpPoint.r=0;
                tmpResult.clear();
                tmpResult.append(tmpPoint);

                resultOut.append(tmpResult);
            }
        }
    }
    if(resultOut.size()>0)
    {
       return 1;
    }

    return 0;
}

int MainWindow::processReceiveData_keba_oneTime(const QString &dataIn, QVector<CameraReturnInfo> &resultOut)
{
//    qDebug()<<"processReceiveData_keba_oneTime";

        //1个数据帧里面可能含有多个物体点　todo
    CameraReturnInfo tmpResult;

    int tmpCount=dataIn.count("\r\n");
    if(0>=tmpCount)
    {
        return -1;
    }
    for(int i=0;i<tmpCount;i++)
    {
        QString tmpStr=dataIn.section("\r\n", i, i);

        if(0==tmpStr.size())
        {
            continue;
        }
        else
        {
    //                tmpStr+="\r";
            if(1==processReceiveData_keba_single(tmpStr,tmpResult))
            {
                resultOut.append(tmpResult);
            }
            else
            {
//                qDebug() << "warn,,,相机反馈数据不符合格式要求"<<tmpStr;
    //                        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 22105, robotId,0,0,tmpStr);
            }
        }
    }
    if(resultOut.size()>0)
    {
       return 1;
    }
    return 0;

}

int MainWindow::processReceiveData_keba_single(QString tmp_frame, CameraReturnInfo &resultOut)
{

//    qDebug()<<"processReceiveData_keba_single"<<tmp_frame;
    //todo 暂时不支持多个同时出现．
    QString pattern = "\\S*\\[X:(-?\\d*\\.*\\d*);Y:(-?\\d*\\.*\\d*);A:(-?\\d*\\.*\\d*)\\S*";
    QRegExp rx(pattern);
//    qDebug()<<rx;

    bool isMatch=rx.exactMatch(tmp_frame);

    if(false==isMatch )
    {
        return 0;
    }
    else
    {
        resultOut.pointCount=1;
        resultOut.x=rx.cap(1).toDouble();
        resultOut.y=rx.cap(2).toDouble();
        resultOut.r=rx.cap(3).toDouble();
    }

    return 1;
}


void MainWindow::on_pushButton_doublenantest_clicked()
{
    double a=1;
    double b=a/0.0;
    if(fabs(b)>1)
    {
        qDebug()<<" test1 "<<fabs(b);
    }
    else
    {
        qDebug()<<" test2 "<<fabs(b);
    }
}

void MainWindow::on_pushButton_prTest_clicked()
{

    QString programStr=ui->lineEdit_source_pr->text();

    QRegExp regExp_prij_const("PR\\[(\\d+),(\\d+)\\]=\\s*(-?\\d*\\.*\\d*)\\s*;\\s*//(.*)");//ok
    QRegExp regExp_pri_pri("PR\\[(\\d+)\\]=\\s*(-?)PR\\[(\\d+)\\]\\s*;\\s*//(.*)");
    QRegExp regExp_pri_minus_pri("PR\\[(\\d+)\\]=\\s*(-?)PR\\[(\\d+)\\]\\s*(-?\\+?)\\s*PR\\[(\\d+)\\]\\s*;\\s*//(.*)");
    QRegExp regExp_prij_prij("PR\\[(\\d+),(\\d+)\\]=\\s*(-*)PR\\[(\\d+),(\\d+)\\]\\s*;\\s*//(.*)");
    QRegExp regExp_prij_minus_prij("PR\\[(\\d+),(\\d+)\\]=\\s*(-*)PR\\[(\\d+),(\\d+)\\]\\s*(-?\\+?)\\s*PR\\[(\\d+),(\\d+)\\]\\s*;\\s*//(.*)");
    QRegExp regExp_prij_prij_const("PR\\[(\\d+),(\\d+)\\]=\\s*(-*)PR\\[(\\d+),(\\d+)\\]\\s*(-?\\+?\\d*\\.*\\d*)\\s*;\\s*//(.*)");



//    programStr.indexOf(rx);

//    bool isMatch=regExp_prij_const.exactMatch(programStr);

//    if(false==isMatch )
//    {
//        qDebug()<<regExp_prij_const.cap(0)<<regExp_prij_const.cap(1)<<regExp_prij_const.cap(2)<<regExp_prij_const.cap(3)<<regExp_prij_const.cap(4)<<regExp_prij_const.cap(5)
//                  <<regExp_prij_const.cap(6)<<regExp_prij_const.cap(7)<<regExp_prij_const.cap(8)<<regExp_prij_const.cap(9);
//        return -1;
//    }
    if(regExp_pri_minus_pri.exactMatch(programStr))//
    {
      qDebug()<<"sucess, regExp_pri_minus_pri"<<regExp_pri_minus_pri;
        return ;
    }
    else if(regExp_prij_minus_prij.exactMatch(programStr))
    {
       qDebug()<<"sucess, regExp_prij_minus_prij"<<regExp_prij_minus_prij;
        return ;
    }
    else if(regExp_prij_prij_const.exactMatch(programStr))
    {
       qDebug()<<"sucess, regExp_prij_prij_const"<<regExp_prij_prij_const;
        return ;
    }
    else if(regExp_prij_const.exactMatch(programStr))
    {
       qDebug()<<"sucess, regExp_prij_const"<<regExp_prij_const;
        return ;
    }
    else if(regExp_prij_prij.exactMatch(programStr))
    {
        qDebug()<<"sucess, regExp_prij_prij"<<regExp_prij_prij;
        return ;
    }
    else if(regExp_pri_pri.exactMatch(programStr))
    {


        qDebug()<<"sucess, regExp_pri_pri"<<regExp_pri_pri;//ok
        return ;
    }
    else
    {
//        qDebug()<<regExp_pri_pri.exactMatch(programStr);//ok
        qDebug()<<regExp_prij_minus_prij.exactMatch(programStr);//ok
        qDebug()<<"error,PR指令正则表达式匹配失败 "<<programStr;
        qDebug()<<regExp_prij_minus_prij<<regExp_prij_minus_prij.cap(0)<<regExp_prij_minus_prij.cap(1)<<regExp_prij_minus_prij.cap(2)
               <<regExp_prij_minus_prij.cap(3)<<regExp_prij_minus_prij.cap(4)<<regExp_prij_minus_prij.cap(5)
                  <<regExp_prij_minus_prij.cap(6)<<regExp_prij_minus_prij.cap(7)<<regExp_prij_minus_prij.cap(8)<<regExp_prij_minus_prij.cap(9);
        return ;
    }

}

void MainWindow::on_pushButton_testKebaVision_clicked()
{
//    QByteArray dataIn=ui->textEdit_visionDataKeba->toPlainText().toLatin1();
//    QByteArray dataIn=ui->plainTextEdit->toPlainText().toLatin1();
//        QByteArray dataIn="Image\r\nDone\r\n";
//           QByteArray dataIn="Image\r\nDone\r\nImage\r\nDone\r\nImage\r\nDone\r\n";
    QByteArray dataIn="Image\r\n[X:253.1;Y:190.02;A:87.2;ATTR:0;ID:8]\r\nDone\r\n";
//    QByteArray dataIn="Image\r\n[X:253.1;Y:190.02;A:87.2;ATTR:0;ID:8]\r\n[X:100.1;Y:220.02;A:333.2;ATTR:0;ID:8]\r\nDone\r\n";
//        QByteArray dataIn="Image\r\n[X:1.1;Y:-2.02;A:3.2;ATTR:0;ID:8]\r\n[X:-4.1;Y:-5.02;A:-6.2;ATTR:0;ID:8]\r\nDone\r\nImage\r\n[X:253.1;Y:190.02;A:87.2;ATTR:0;ID:8]\r\n[X:100.1;Y:220.02;A:333.2;ATTR:0;ID:8]\r\nDone\r\n";
    QVector<QVector<CameraReturnInfo> >  resultOut;
    processReceiveData_keba(dataIn,resultOut);

    qDebug()<<"result print";
    for(int i=0;i<resultOut.size();i++)
    {
        qDebug()<<"i"<<i;
        for(int j=0;j<resultOut[i].size();j++)
        {
            qDebug()<<"j"<<j<<"[x"<<resultOut[i][j].x<<"y"<<resultOut[i][j].y<<"r"<<resultOut[i][j].r<<"]"
                   <<resultOut[i][j].pointCount;
        }
    }
}
